<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>

	<p:processor name="oxf:unsafe-xslt">
		<p:input name="data" href="#request#xpointer(/request/parameters)"/>
		<p:output name="data" id="parameters"/>
		<p:input name="config">
			<xsl:stylesheet version="2.0">
				<xsl:template match="/">
					<parameters>
						<xsl:apply-templates/>
					</parameters>
				</xsl:template>
				<xsl:template match="parameter">
					<xsl:element name="{name}">
						<xsl:value-of select="value"/>
					</xsl:element>
				</xsl:template>
			</xsl:stylesheet>
		</p:input>
	</p:processor>
	<p:processor name="oxf:xslt">
		<p:input name="data" href="#parameters"/>
		<p:input name="session" href="#session"/>
		<p:output name="data" id="wgt_pref_query"/>
		<p:input name="config">
			<xsl:stylesheet version="2.0">
				<xsl:template match="/">
					<!--Database modification-->
					<xsl:if test="doc('input:session')/session/groups/group[@id=string(doc('input:session')/session/groups/@current)]/@level != '-1'">
						<xdb:update collection="/db/portal">
							<xu:modifications version="1.0">
								<xu:variable name="Upath" select="{if (/session/groups/@current = 'gr_0') then concat('/users/user[@id=''',doc('input:session')/session/username,''']') else concat('/groups/group[@id=''',doc('input:session')/session/groups/@current,''']')}/interface/widget[@id = '{/parameters/widgetId}']"/>

								<xu:variable name="Wpath" select="/widgetsList/category/widget[@id = '{/parameters/widgetId}']"/>
								<!--create preferences element-->
								<xu:if test="not($Upath/preferences)">
									<xu:append select="$Upath">
										<preferences/>
									</xu:append>
								</xu:if>
								<xsl:apply-templates select="/parameters/*"/>
							</xu:modifications>
						</xdb:update>
					</xsl:if>
					<!--Session Modification (Not used anymore)-->
					<xsl:if test="doc('input:session')/session/groups/group[@id=string(doc('input:session')/session/groups/@current)]/@level = '-1'">

						<xu:modifications version="1.0">
							<xu:variable name="Upath" select="{if (/session/groups/@current = 'gr_0') then concat('/users/user[@id=''',doc('input:session')/session/username,''']') else concat('/groups/group[@id=''',doc('input:session')/session/groups/@current,''']')}/interface/widget[@id = '{/parameters/widgetId}']"/>

							<!--create preferences element-->
							<xu:if test="not($Upath/preferences)">
								<xu:append select="$Upath">
									<preferences/>
								</xu:append>
							</xu:if>
							<xsl:apply-templates select="/parameters/*[name(.) != 'widgetTags']"/>
						</xu:modifications>
					</xsl:if>
				</xsl:template>
				<xsl:template match="widgetId">
				</xsl:template>
				<xsl:template match="widgetTags">
					<xsl:variable name="tags" select="tokenize(upper-case(normalize-space(.)), ' ')"/>
					<xu:if test="not($Wpath/tags)">
						<xu:append select="$Wpath">
							<tags/>
						</xu:append>
					</xu:if>
					<xsl:for-each select="$tags">
						<!--Toggle tag-->
						<xu:variable name="exist" select="$Wpath/tags/tag[. = '{.}']"/>
						<xu:if test="$exist">
							<xu:remove select="$Wpath/tags/tag[. = '{.}']"/>
						</xu:if>
						<xu:if test="not($exist)">
							<xu:append select="$Wpath/tags">
								<tag>
									<xsl:value-of select="."/>
								</tag>
							</xu:append>
						</xu:if>
					</xsl:for-each>
				</xsl:template>
				<xsl:template match="*">
					<xu:if test="$Upath/preferences/{node-name(.)}">
						<xu:update select="$Upath/preferences/{node-name(.)}">
							<xsl:value-of select="."/>
						</xu:update>
					</xu:if>
					<xu:if test="not($Upath/preferences/{node-name(.)})">
						<xu:append select="$Upath/preferences">
							<xu:element name="{node-name(.)}">
								<xsl:value-of select="."/>
							</xu:element>
						</xu:append>
					</xu:if>
				</xsl:template>
			</xsl:stylesheet>
		</p:input>
	</p:processor>
	<p:choose href="#session">
		<p:when test="/session/groups/@current = 'gr_0' or /session/groups/group[@id=string(/session/groups/@current)]/@level = '2' or true()">


			<p:processor name="oxf:xmldb-update">
				<p:input name="datasource" href="/config/datasource.xml"/>
				<p:input name="query" href="#wgt_pref_query"/>
			</p:processor>

			<p:processor name="oxf:pipeline">
				<p:input name="config" href="utils/session_rewriter.xpl"/>
			</p:processor>
		</p:when>
		<!--		Simple User (Not Used anymore)-->
		<p:otherwise>
			<p:processor name="oxf:xupdate">
				<p:input name="data" href="#session#xpointer(/session/groups)"/>
				<p:output name="data" id="new_groups"/>
				<p:input name="config" href="#wgt_pref_query"/>
			</p:processor>
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#session"/>
				<p:input name="groups" href="#new_groups"/>
				<p:output name="data" id="new_session"/>
				<p:input name="config">
					<xsl:stylesheet version="2.0">
						<xsl:template match="/">
							<xsl:apply-templates/>
						</xsl:template>
						<xsl:template match="groups">
							<xsl:copy-of select="doc('input:groups')"/>
						</xsl:template>
						<xsl:template match="@*|node()" priority="-2">
							<xsl:copy>
								<xsl:apply-templates select="@*|node()"/>
							</xsl:copy>
						</xsl:template>
					</xsl:stylesheet>
				</p:input>
			</p:processor>
			<p:processor name="oxf:scope-serializer">
				<p:input name="config">
					<config>
						<key>session</key>
						<scope>session</scope>
						<session-scope>application</session-scope>
					</config>
				</p:input>
				<p:input name="data" href="#new_session"/>
			</p:processor>
		</p:otherwise>
	</p:choose>

	<!--<p:processor name="oxf:xml-converter">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<encoding>utf-8</encoding>
				<version>1.0</version>
				<omit-xml-declaration>true</omit-xml-declaration>
			</config>
		</p:input>
		<p:input name="data" href="#session"/>
		<p:output name="data" id="xml-doc"/>
	</p:processor>

	<p:processor name="oxf:http-serializer">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<force-content-type>true</force-content-type>
			</config>
		</p:input>
		<p:input name="data" href="#xml-doc"/>
	</p:processor>-->

</p:config>